/*
 * Created by JFormDesigner on Sat Dec 26 15:44:29 CST 2020
 */

package cn.fourhundredandeighteen.view.AdminView.RewardView;

import cn.fourhundredandeighteen.controller.StudentController;
import cn.fourhundredandeighteen.controller.SubjectController;
import cn.fourhundredandeighteen.entity.Course;
import cn.fourhundredandeighteen.entity.Subject;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;

/**
 * @author Br.S
 */
public class rewardInfoChang extends JPanel {
    private Long id;
    public rewardInfoChang(Long id) {
        this.id = id;
        initComponents();
        SubjectController subjectController = new SubjectController();
        Result<Subject> subjectOne = subjectController.getSubjectOne(this.id);
        textField1.setText(String.valueOf(subjectOne.getData().getSubjectId()));
        textField1.setEditable(false);
        textField2.setText(String.valueOf(subjectOne.getData().getSubjectName()));
        textField3.setText(String.valueOf(subjectOne.getData().getSubjectGrade()));
        textField4.setText(String.valueOf(subjectOne.getData().getSubjectLevel()));
        textField5.setText(String.valueOf(subjectOne.getData().getSubjectCategory()));
        textField6.setText(String.valueOf(subjectOne.getData().getSubjectCount()));
        textField7.setText(String.valueOf(subjectOne.getData().getCreateTime()));
        textField7.setEditable(false);
        this.updateUI();
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here

        try{
            Subject subject = new Subject();
            subject.setSubjectId(id);
            subject.setSubjectName(textField2.getText());
            subject.setSubjectGrade(textField3.getText());
            subject.setSubjectLevel(Integer.valueOf(textField4.getText()));
            subject.setSubjectCategory(textField5.getText());
            subject.setSubjectCount(Integer.valueOf(textField6.getText()));
            subject.setUpdateTime(new Date());
            SubjectController subjectController = new SubjectController();
            Long[] longs = new Long[Integer.valueOf(textField6.getText())];
            for (int i = 0; i < Integer.valueOf(textField6.getText()); i++) {
                String inputDialog = JOptionPane.showInputDialog("请输入学号");
                longs[i]= Long.valueOf(inputDialog);
            }

            Result result1 = subjectController.setSubjectUsers(id, longs);
            System.out.println(result1);
            if (result1.getMassage()=="SUCCESS"){
                Result result = subjectController.upDataSubject(subject);
                if (result.getMassage()=="SUCCESS"){
                    JOptionPane.showMessageDialog(null, "修改成功");
                    Container.getInstance().upDatePage();
                }
            }

        }catch (Exception exception){
            JOptionPane.showMessageDialog(null, "请检查参数");
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        label4 = new JLabel();
        textField4 = new JTextField();
        label5 = new JLabel();
        textField5 = new JTextField();
        label6 = new JLabel();
        textField6 = new JTextField();
        label7 = new JLabel();
        textField7 = new JTextField();
        button1 = new Button();
        label9 = new JLabel();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {87, 69, 0, 0, 207, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {43, 55, 55, 51, 55, 55, 54, 55, 37, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label1 ----
        label1.setText("Id");
        label1.setMaximumSize(new Dimension(23, 25));
        add(label1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField1, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label2 ----
        label2.setText("\u6bd4\u8d5b\u540d\u79f0");
        add(label2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField2, new GridBagConstraints(2, 2, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label3 ----
        label3.setText("\u83b7\u5956\u7ea7\u522b");
        add(label3, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField3, new GridBagConstraints(2, 3, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label4 ----
        label4.setText("\u6bd4\u8d5b\u7ea7\u522b");
        add(label4, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField4, new GridBagConstraints(2, 4, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label5 ----
        label5.setText("\u6bd4\u8d5b\u7c7b\u522b");
        add(label5, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField5, new GridBagConstraints(2, 5, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label6 ----
        label6.setText("\u4eba\u6570");
        add(label6, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField6, new GridBagConstraints(2, 6, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label7 ----
        label7.setText("\u6bd4\u8d5b\u65f6\u95f4");
        add(label7, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField7, new GridBagConstraints(2, 7, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- button1 ----
        button1.setText("\u4fee\u6539");
        button1.addActionListener(e -> button1ActionPerformed(e));
        add(button1, new GridBagConstraints(2, 8, 2, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 15), 0, 0));
        add(label9, new GridBagConstraints(4, 8, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JLabel label4;
    private JTextField textField4;
    private JLabel label5;
    private JTextField textField5;
    private JLabel label6;
    private JTextField textField6;
    private JLabel label7;
    private JTextField textField7;
    private JButton button1;
    private JLabel label9;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
